package com.jincheng.springblog.utils;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;

public class SecurityUtils {

    // inputPassword
    public static String encrypt(String Password){
        String salt = UUID.randomUUID().toString().replace("-","");
        String finalPassword = DigestUtils.md5DigestAsHex((Password + salt).getBytes());
        return salt + finalPassword;
    }

    public static boolean verify(String inputPassword, String sqlPassword){
        if(!StringUtils.hasLength(inputPassword)){
            return false;
        }
        if(sqlPassword == null || sqlPassword.length() != 64){
            return false;
        }
        String salt = sqlPassword.substring(0, 32);
        String finalPassword = DigestUtils.md5DigestAsHex((inputPassword + salt).getBytes());

        return (salt + finalPassword).equals(sqlPassword);
    }



}
